package com.company;

import java.lang.reflect.Method;

public class ReflectDemo04 {
    public static void main(String[] args) throws Exception {
        Person p1 = new Person("p1",11);
        Person p2 = new Person("p2",22);
        // 1. 获取类的对象

        // 2. 只能 获取Public 修饰的
        Class cls = Person.class;
        Method mt = cls.getMethod("getName");
        Object val1 = mt.invoke(p1);  //name = p1     age = 11
        System.out.println(val1);  //p1

         Method mt1 = cls.getMethod("setName", String.class);
        Object val2 = mt1.invoke(p1, "我是p1");  //name = 我是p1     age = 11
        System.out.println(val2);  //null

        // 3. 获取任意权限符修饰的方法
        Method mt2 = cls.getDeclaredMethod("doing", String.class);
        mt2.setAccessible(true);
        Object val3 = mt2.invoke(p1,"吃饭");  //正在  吃饭
        System.out.println(val3);  //吃饭


        // 4 获取所有的 任意权限符修饰的方法
        Method[] mtArr = cls.getDeclaredMethods();
        for (Method met : mtArr){
            System.out.println(met);
            /*
            public java.lang.String com.company.Person.toString()
            public java.lang.String com.company.Person.getName()
            public void com.company.Person.setName(java.lang.String)
            private java.lang.String com.company.Person.doing(java.lang.String)
            public int com.company.Person.getAge()
            public void com.company.Person.setAge()
             */
        }

    }
}
